package com.niiwoo.civet.mobile.lanmao.handler;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.niiwoo.civet.account.dto.request.BackrollRechargeRequestDTO;
import com.niiwoo.civet.account.service.WithdrawDubboService;
import com.niiwoo.tripod.lanmao.enums.LanMaoGatewayServiceNameEnum;
import com.niiwoo.tripod.lanmao.notify.BackrollRechargeNotify;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 提现失败资金回冲通知
 * Created by wangjichao on 2017/12/11.
 */
@Slf4j
@Component
public class BackrollRechargeHandler implements LanMaoNotifyHandler{

    @Reference(version = "1.0.0")
    private WithdrawDubboService withdrawDubboService;

    @Override
    public boolean supports(String serviceName) {
        return LanMaoGatewayServiceNameEnum.BACKROLL_RECHARGE.name().equals(serviceName);
    }

    @Override
    public HandleResult handle(JSONObject json) {
        if (json == null) {
            log.error("资金回退充值Handler,json is null");
        }
        log.info("资金回退充值Handler,requestJson:{}",json.toJSONString());
        HandleResult handleResult = new HandleResult();
        handleResult.setNeedAck(false);
        BackrollRechargeNotify notify = JSON.parseObject(json.toJSONString(), BackrollRechargeNotify.class);
        BackrollRechargeRequestDTO requestDTO = new BackrollRechargeRequestDTO();
        requestDTO.setRequestNo(notify.getRequestNo());
        requestDTO.setCompletedTime(notify.getCompletedTime());
        requestDTO.setRollbackAmount(notify.getRollbackAmount());
        requestDTO.setRollbackCommission(notify.getRollbackCommission());
        requestDTO.setRollbackType(notify.getRollbackType());
        requestDTO.setWithdrawRequestNo(notify.getWithdrawRequestNo());
        withdrawDubboService.backrollRechargeNotify(requestDTO);
        handleResult.setNeedAck(true);
        log.info("资金会退充值Handler,handleResult:{}", JSON.toJSONString(handleResult));
        return handleResult;
    }
}
